# 一个简单的TCP客户端，用于连接到指定的服务器并发送HTTP请求
import socket

def main():
    server_name = '127.0.0.1'
    server_port = 50008
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((server_name, server_port))

    print("Input the http request (end with an empty line):")
    sentence = ""
    while True:
        tmp = input()
        if tmp == "":
            break
        sentence += tmp + '\r\n'  # 每一行后面加上换行符

    # 发送HTTP请求并添加结束标志
    request = sentence.encode() + b'\r\n'  # 将整个请求编码为字节串，并添加最后的换行符
    client_socket.sendall(request)

    receive_sentence = b""
    while True:
        data = client_socket.recv(1024)
        if not data:
            break
        receive_sentence += data

    print("From Server:\n", receive_sentence.decode())

    client_socket.close()

if __name__ == "__main__":
    main()